<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>文章列表</title>
    <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="./css/reset.css">
    <link rel="stylesheet" href="./css/iconfont.css">
    <link rel="stylesheet" href="./css/main.css">
    <script src="./libs/jquery-1.12.4.min.js"></script>
    <!-- 引入模板引擎js文件 -->
    <script src="./libs/template-web.js"></script>

    <script src="./libs/https.js"></script>

    <script src="./libs/jquery.twbsPagination.js"></script>
</head>

<body>
    <div class="container-fluid">
        <div class="common_title">
            文章列表
        </div>
        <div class="container-fluid common_con">
            <div class="row opt_btns">
                <div class="col-xs-6">
                    <form class="form-inline">
                        <select id="selCategory" name="" class="form-control input-sm">
                            <option value="">所有分类</option>
                            <option>未分类</option>
                            <option>奇趣事</option>
                            <option>会生活</option>
                            <option>爱旅行</option>
                        </select>
                        <select id="selStatus" name="" class="form-control input-sm">
                            <option value="">所有状态</option>
                            <option value="草稿">草稿</option>
                            <option value="已发布">已发布</option>
                        </select>
                        <button id="btnSearch" class="btn btn-default btn-sm">筛选</button>
                    </form>
                </div>
                <div class="col-xs-6">
                    <a href="article_release.html" class="btn btn-success btn-sm pull-right" id="release_btn">发表文章</a>
                </div>
            </div>
            <table class="table table-striped table-bordered table-hover mp20">
                <thead>
                    <tr>
                        <th>标题</th>
                        <th>作者</th>
                        <th>分类</th>
                        <th class="text-center">发表时间</th>
                        <th class="text-center">状态</th>
                        <th class="text-center" width="100">操作</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>王积千造统最头</td>
                        <td>杰克</td>
                        <td>奇趣事</td>
                        <td class="text-center">2017-06-08 07:08:46</td>
                        <td class="text-center">已发布</td>
                        <td class="text-center">
                            <a href="article_edit.html" class="btn btn-default btn-xs">编辑</a>
                            <a href="javascript:void(0);" class="btn btn-danger btn-xs delete">删除</a>
                        </td>
                    </tr>
                </tbody>
            </table>
            <div class="row text-center">
                <ul id="pagination" class="pagination-sm"></ul>
            </div>
        </div>
    </div>
    <!-- 准备下拉框的模板 -->
    <script type="text/html" id="tpl-category">
        <option value="">所有分类</option>
        {{each data}}

        <option value="{{$value.id}}">{{$value.name}}</option>

        {{/each}}
    </script>

    <!--准备内容模板  -->
    <script type="text/html" id="tpl-conten">
        {{ each data.data }}
        <tr>
            <td>{{ $value.title }}</td>
            <td>{{ $value.author }}</td>
            <td>{{ $value.category }}</td>
            <td class="text-center">{{ $value.date }}</td>
            <td class="text-center">{{ $value.state }}</td>
            <td class="text-center">
                <a href="article_edit.html?articleId={{$value.id}}" class="btn btn-default btn-xs"
                    data-id="{{ $value.id}}">编辑</a>
                <a href="javascript:void(0);" class="btn btn-danger btn-xs delete" data-id="{{ $value.id}}">删除</a>
            </td>
        </tr>
        {{ /each }}
    </script>
    <script>
        $(function () {

            $.ajax({
                type: 'get',
                url: BigNew.category_list,
                success: function (backData) {
                    if (backData.code == 200) {
                        let htmls = template('tpl-category', backData)
                        $('#selCategory').html(htmls)
                    }
                }
            });

            let setPage = 1
            //文章分类搜索
            function loadData(page) {
                setPage = page
                $.ajax({
                    type: 'get',
                    url: BigNew.article_query,
                    data: {
                        type: $('#selCategory').val(),
                        state: $('#selStatus').val(),
                        //初始页
                        page: page,
                        //一页显示多少条数据
                        perpage: 5,
                    },
                    success: function (backData) {
                        if (backData.code == 200) {
                            // console.log(backData);
                            if (backData.data.totalCount > 0) {
                                let htmls = template('tpl-conten', backData)
                                $('tbody').html(htmls)

                                //为了解决只有第一次甚至总页码有效的问题，先摧毁原来的设置
                                $('#pagination').twbsPagination('destroy')
                                //    //分页导航栏
                                $('#pagination').twbsPagination({

                                    //总页数
                                    totalPages: backData.data.totalPage,

                                    //当前显示的页
                                    startPage: page,
                                    //阻止初始化的点击事件
                                    initiateStartPageClick: false,

                                    first: '首页',
                                    last: '尾页',
                                    next: '下一页',
                                    prev: '上一页',

                                    visiblePages: 5,
                                    onPageClick: function (event, page) {
                                        // console.log(page);
                                        //将点击的页码作为参数传给函数
                                        loadData(page)
                                    }
                                })
                            } else {
                                $('tbody').empty()
                                $('#pagination').html('当前类别没有任何数据····')
                            }
                        }
                    }
                })
            }
            //首次调用显示的是第一页
            loadData(1)

            //设置筛选按钮
            $('#btnSearch').on('click', function (e) {
                e = window.event || e
                e.preventDefault()
                loadData(1)
            })


            //删除按钮
            $('tbody').on('click', '.delete', function () {
                let delid = $(this).attr('data-id')
                // console.log(delid);
                $.ajax({
                    type: 'post',
                    url: BigNew.article_delete,
                    data: {
                        id: delid
                    },
                    success: function (backData) {
                        if (backData.code == 204) {
                            // console.log(backData);
                            //解决删除最后一页最后一条的bug
                            if ($('tbody tr').length > 1) {
                                loadData(setPage)
                            } else {
                                loadData(setPage - 1)
                            }
                        }
                    }
                })
            })
        })
    </script>
</body>

</html>